<!-- src/components/GlobalSearch.vue -->
<template>
  <div class="global-search" @click="handleClick">
    <i class="iconfont">&#xe662;</i>
    <span>搜索</span>
  </div>
  <transition name="slide">
    <SearchBox v-if="isVisible" @search="handleSearch" />
  </transition>
  <ItemList v-if="searchQuery" :search-query="searchQuery" />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import SearchBox from './Search.vue';
import ItemList from './ItemList.vue';

const isVisible = ref(false);
const searchQuery = ref('');

// const itemList = ref<InstanceType<typeof ItemList> | null>(null);

const handleClick = () => {
  if (isVisible.value) {
    // 如果搜索框已经显示，再次点击则关闭搜索框和搜索结果
    isVisible.value = false;
    searchQuery.value = '';
  } else {
    // 如果搜索框未显示，则显示搜索框
    isVisible.value = true;
  }
};
const handleSearch = (query: string) => {
  searchQuery.value = query; // 更新搜索查询
};
</script>

<style scoped lang="scss">
.global-search {
  position: fixed;
  bottom: 79px;
  right: 20px;
  background-color: #fff;
  border: 1px solid #ccc;
  padding: 10px;
  border-radius: 8px;
  /* 圆角 */
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  /* 文字在图标下面 */
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  /* 更深的阴影 */
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
  /* 添加过渡效果 */

  i {
    font-size: 12px;
    /* 增加图标的大小 */
    color: #0d1216;
    transition: transform 0.3s ease;
    /* 添加图标放大过渡效果 */
  }

  span {
    font-size: 12px;
    color: #0d1216;
    margin-top: 5px;
    /* 图标和文字之间的间距 */
  }

  &:hover {
    background-color: #f0f0f0;
    transform: scale(1.1);

    /* 悬浮时放大按钮 */
    i {
      transform: scale(1.2);
      /* 悬浮时放大图标 */
    }
  }
}
</style>
